import json
import requests

# Set your API endpoint and the dataset_id and api_key
url = 'https://api.dify.ai/v1/datasets/{dataset_id}/document/create-by-file'
headers = {
    'Authorization': 'Bearer {api_key}'
}

# Define the data as a Python dictionary
data_dict = {
    "indexing_technique": "high_quality",
    "process_rule": {
        "rules": {
            "pre_processing_rules": [
                {"id": "remove_extra_spaces", "enabled": True},
                {"id": "remove_urls_emails", "enabled": True}
            ],
            "segmentation": {
                "separator": "###",
                "max_tokens": 500
            }
        },
        "mode": "custom"
    }
}

# Convert the dictionary to a JSON string
data = {
    'data': json.dumps(data_dict)
}

# Define the file to be uploaded
files = {
    'file': open('/path/to/file', 'rb')  # Ensure the file path is correct
}

# Make the POST request
response = requests.post(url, headers=headers, data=data, files=files)

# Check the response status
if response.status_code == 200:
    print('File uploaded successfully:', response.json())
else:
    print('Error uploading file:', response.status_code, response.text)

# Close the file after uploading
files['file'].close()
